package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IProductTypeDao;
import com.cn.wanxi.model.ProductTypeModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductTypeDaoImpl implements IProductTypeDao {
    @Override
    public List<ProductTypeModel> getProductTypeList() {
        String sql = "select * from product_type";
        List<ProductTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
            try {
                while (rs.next()) {
                    ProductTypeModel productType = new ProductTypeModel();
                    productType.setId(rs.getInt("id"));
                    productType.setHref(rs.getString("href"));
                    productType.setName(rs.getString("name"));
                    productType.setCreateTime(rs.getString("create_time"));
                    productType.setUpdateTime(rs.getString("update_time"));
                    productType.setEnable(rs.getInt("enable"));
                    productType.setName(rs.getString("name"));
                    list.add(productType);
                }
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
    }

    @Override
    public ProductTypeModel getProductType(int id) {
        String sql = "select * from product_type where id="+id;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        ProductTypeModel productType = null;
        try {
            while (rs.next()) {
                productType = new ProductTypeModel();
                productType.setId(rs.getInt("id"));
                productType.setHref(rs.getString("href"));
                productType.setName(rs.getString("name"));
                productType.setCreateTime(rs.getString("create_time"));
                productType.setUpdateTime(rs.getString("update_time"));
                productType.setEnable(rs.getInt("enable"));
                productType.setName(rs.getString("name"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productType;
    }

    @Override
    public int addProductType(ProductTypeModel productType) {
        String sql = "insert into product_type(name,href,create_time) values('"
                + productType.getName() +"','"+ productType.getHref()
                +"','"+ productType.getCreateTime()
                +"')";
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteProductType(int id) {
        String sql = "delete from product_type where id=" +id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public int updateProductType(ProductTypeModel productType) {
        String sql = "update product_type set name='"+ productType.getName()
                +"',href='"+ productType.getHref()
                +"',update_time='"+ productType.getUpdateTime()
                +"' where id="+productType.getId();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductTypeModel> getProductTypeList(int page, int size) {
        String sql = "select * from product_type limit "+ (page-1)*size +","+size;
        List<ProductTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()) {
                ProductTypeModel productType = new ProductTypeModel();
                productType.setId(rs.getInt("id"));
                productType.setHref(rs.getString("href"));
                productType.setName(rs.getString("name"));
                productType.setCreateTime(rs.getString("create_time"));
                productType.setUpdateTime(rs.getString("update_time"));
                productType.setEnable(rs.getInt("enable"));
                productType.setName(rs.getString("name"));
                list.add(productType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductTypeModel> getProductTypeList(int page, int size, String sort) {
        String sql = "select * from product_type order by "+ sort +" limit "+ (page-1)*size +","+size;
        List<ProductTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()) {
                ProductTypeModel productType = new ProductTypeModel();
                productType.setId(rs.getInt("id"));
                productType.setHref(rs.getString("href"));
                productType.setName(rs.getString("name"));
                productType.setCreateTime(rs.getString("create_time"));
                productType.setUpdateTime(rs.getString("update_time"));
                productType.setEnable(rs.getInt("enable"));
                productType.setName(rs.getString("name"));
                list.add(productType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductTypeModel> searchProductType(String sql) {
        List<ProductTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()) {
                ProductTypeModel productType = new ProductTypeModel();
                productType.setId(rs.getInt("id"));
                productType.setHref(rs.getString("href"));
                productType.setName(rs.getString("name"));
                productType.setCreateTime(rs.getString("create_time"));
                productType.setUpdateTime(rs.getString("update_time"));
                productType.setEnable(rs.getInt("enable"));
                productType.setName(rs.getString("name"));
                list.add(productType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public long getProductTypeCount() {
        String sql = "select count(*) from product_type";
        return JDBCUtils.getCount(sql);
    }
}
